package com.guoguo.ztree.service.impl;

import com.guoguo.ztree.entity.Group;
import com.guoguo.ztree.entity.Paging;
import com.guoguo.ztree.service.GroupService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Group> getGroupListByParentId(Long parentId) {
        String sql = "select id,`name`,(select ifnull(count(1),0) from `group` childGroup where childGroup.parent_id=`group`.id) childCount from `group` where parent_id=?";
        return jdbcTemplate.query(sql, new Object[]{parentId}, new BeanPropertyRowMapper(Group.class));
    }

    @Override
    public int[] batchAdd() {
        String sql = "insert into `group` (`name`,parent_id) values (?,?)";

        List<Object[]> batchArgs = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            batchArgs.add(new Object[]{"group1-" + i + 1, 50001});
        }
        for (int i = 0; i < 5; i++) {
            batchArgs.add(new Object[]{"group2-" + i + 1, 50006});
        }

        return jdbcTemplate.batchUpdate(sql, batchArgs);
    }

    /**
     * 查询分页（MySQL数据库）
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Paging<Group> getGroupListPage(Group group, long pageNum, int pageSize) {
        group.setParentId(group.getParentId() == null ? 0 : group.getParentId());

        Long totalRowCount = getGroupCount(group);

        Long totalPageNum = totalRowCount % pageSize == 0 ? totalRowCount / pageSize : totalRowCount / pageSize + 1;
        pageNum = pageNum < 1 ? 1 : pageNum;
        pageNum = totalPageNum > 0 && pageNum > totalPageNum ? totalPageNum : pageNum;

        List<Object> parameter = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select id,`name`,(select ifnull(count(1),0) from `group` childGroup where childGroup.parent_id=`group`.id) childCount from `group` where parent_id=?");
        parameter.add(group.getParentId());
        if (StringUtils.isNotBlank(group.getName())) {
            sql.append(" and `name` like CONCAT('%',?,'%')");
            parameter.add(group.getName());
        }
        sql.append(" limit ?,?");
        parameter.add((pageNum - 1) * pageSize);
        parameter.add(pageSize);

        List<Group> list = jdbcTemplate.query(sql.toString(), parameter.toArray(), new BeanPropertyRowMapper(Group.class));

        return new Paging(pageNum, pageSize, totalRowCount, totalPageNum, list);
    }

    /**
     * 获取部门总数据数
     *
     * @param group
     * @return
     */
    public Long getGroupCount(Group group) {
        List<Object> parameter = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select ifnull(count(1),0) from `group` where parent_id=?");
        parameter.add(group.getParentId());
        if (StringUtils.isNotBlank(group.getName())) {
            sql.append(" and `name` like CONCAT('%',?,'%')");
            parameter.add(group.getName());
        }
        return jdbcTemplate.queryForObject(sql.toString(), parameter.toArray(), Long.class);
    }
}
